/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */
import React, {Component} from 'react';
import {Alert, DeviceEventEmitter, StyleSheet, Text, TextInput, TouchableHighlight, View} from 'react-native';
import Toast from './component/Toast';
import getStyles from '../style/styles';
import axiosInstance from './utils/request';


let styles={};
export default class CustInfoEdit extends Component {


  constructor(props){
  	super(props);
    this.state = {
		userType:global.user.userData.userType,
		custCode:'',
		field:'',
		value:''
    }
  }

  	//组件挂载完成后执行的方法
  	componentDidMount(){

  		this.props.navigation.setOptions({
			title: '用户信息更新',
		})
		const { params } = this.props.route
	  	this.setState({
	  		field:params.name,
			value:params.value,
			custCode:params.custCode
	  	})
  	}

    saveData(){
		let params=[];
		let custCode=this.state.custCode;
		let field=this.state.field;
		let value=this.state.value;
		if(field=='详细地址'){
			params = {
				custCode:custCode,
				address:value,
			};
		}else if(field=='联系电话'){
			params = {
				custCode:custCode,
				tel:value,
			};
		}else if(field=='客户名称'){
			params = {
				custCode:custCode,
				custName:value,
			};
		}
		this.update(params);
	}

	update(data){
		let url=global.server.url+'/custInfo/update';
		axiosInstance({
		  method: 'post',
		  url: url,
		  data: data
		})
		.then(res => {
			if(res.data){
				this.goBack(data);
			}else{
				Toast.show('修改失败！');
			}
		 })
		 .catch(error => {
			Toast.show("保存数据时发生异常，请联系系统管理员:"+error);
		 })
	}

	goBack(data){
		DeviceEventEmitter.emit('Key', {custCode:data.custCode});
		this.props.navigation.navigate('CustInfoEditScene',{custCode:data.custCode})
	}

 	saveTips = () => {
        Alert.alert(
            '提示', //提示标题
            '确定修改该数据吗？', //提示内容
            [
                {
                 	text: '确定',
				  	onPress: ()=> this.saveData()
				},
				{
					text: '取消',
					onPress: () => console.log('点击取消')
				}
            ] //按钮集合
        )
   }

   setValue(text) {
	   this.setState({
			value: text
	   });
	}

    render() {
		styles=getStyles();
        return (
			<View style={style.container}>
				<View style={styles.items}>
					<Text style={styles.textStyle}>{this.state.field}:</Text>
					<TextInput
						underlineColorAndroid="gray"
						placeholder={this.state.field}
						autoFocus={false}
						clearButtonMode="while-editing"
						style={styles.inputStyle}
						value={this.state.value+''}
						onChangeText={(text)=>this.setValue(text)}>
					</TextInput>
				</View>
				{//如果为管理员，则显示保存按钮
                 	this.state.userType == "0" ? (
						<View>
							<TouchableHighlight
								style={style.saveBtn}
								underlayColor='transparent'
								onPress={() => this.saveTips()}>
								<Text style={styles.saveText}>保存</Text>
							</TouchableHighlight>
						</View>
                      ) : (
					 	null
                     )
                 }
			</View>
		)
    }
}

const style = StyleSheet.create({
    container: {
        flex: 1,
		flexDirection: 'column',
        backgroundColor: 'white'
    },
    saveBtn: {
        height: 35,
		width:60,
        backgroundColor: 'green',
        margin: 4,
        justifyContent: 'center',
		borderRadius:5,
		alignSelf:'center'
    },
});
